package com.scfire.util

import groovyx.net.http.RESTClient
import net.sf.json.JSONArray
import net.sf.json.JSONObject
/** 
 * Copyright 2015 Shudao Info Tech Co. Ltd.  All rights reserved.
 * @author lhzeng
 * @email zenglh@haoshudao.com
 * @date 2015年6月7日 下午2:36:26 
 * @version V1.0
 */

class BaiduMapAPI {
	/**
	 * 百度地图坐标转换API
	 */
	public static String[] geoconv(double longitude, double latitude){
		String url = "http://api.map.baidu.com/geoconv/v1/?coords=" + longitude + "," + latitude + "&from=3&to=5&output=json&ak=LmMtgXZasqSzNgussRmEQdjK"
		String res = HttpUtil.getHttpGetResponse(url,null)
		String [] rtnStrs = new String[2];
		try {
			JSONObject jso = JSONObject.fromObject(res)
			JSONArray ja = jso.get("result")
			JSONObject jol = ja[0]
			rtnStrs[0] = jol.get("x")
			rtnStrs[1] = jol.get("y")
			return rtnStrs
		} catch (Exception e) {
			e.printStackTrace()
		}
	}
	
	/**
	 * 将百度地图的经纬度坐标转换成地点
	 * @param longitude
	 * @param latitude
	 * @return
	 */
	public static String geoCoder(double longitude, double latitude){
		String url = "http://api.map.baidu.com/geocoder/v2/?ak=LmMtgXZasqSzNgussRmEQdjK&callback=renderReverse&location=" + latitude + "," + longitude + "&output=json";
		String res = HttpUtil.getHttpGetResponse(url,null)
		try{
			String subStr = res.substring(res.indexOf("(")+1, res.lastIndexOf(")"))
			JSONObject jso = JSONObject.fromObject(subStr)
			String address = jso.get("result").get("formatted_address")
			return address
		}
		catch(Exception e){
			e.printStackTrace()
		}
	}
	
	static main(args) {
		String[] strs = this.geoconv(121.584557, 31.1838)
		println geoCoder(Double.parseDouble(strs[0]),Double.parseDouble(strs[1]))
	}

}
 